package com.ml.mall.api.common;

import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.file.FileUploadDto;
import com.ml.mall.bean.req.file.ImageDto;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.enums.RetStatusNum;
import com.ml.mall.exception.BizException;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.file.FileService;
import com.ml.mall.utils.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @description    :文件上传接口
 * @author         :zxm
 * @date           :2021/5/12
**/
@RestController
@Api(tags = "【auth】文件传输")
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传接口")
////  @RequiresPermissions(PermissionConst.FILE_UPLOAD_FILE) //放行，不起用
    public ApiRestResult multipartFile(FileUploadDto dto, MultipartFile[] mf)  {
        log.info("uploadFile=======who=upload={}", JwtUtil.getCurrentUser().getAcc());
        switch (dto.getNum()){
            case 0:
                return ApiRestResult.ok( fileService.uploadFile(mf, "logo") );
            case 1:
                return ApiRestResult.ok( fileService.uploadFile(mf,"head") );
            case 2:
                //产品图片
                return ApiRestResult.ok( fileService.uploadProdFile(mf,dto,"prod"));
            case 4:
                //产品图片 目录图片
                return ApiRestResult.ok( fileService.uploadFile(mf,"category") );
            case 5:
                //公司证书;多张
                return ApiRestResult.ok( fileService.uploadProdFile(mf,dto,"company_ssm") );
            case 6:
                //公司证书;多张
                return ApiRestResult.ok( fileService.uploadProdFile(mf,dto,"banner"));
            default:
                throw new BizException( CommonEnum.ILLEGAL_REQ_ERROR );
        }
    }
    @PostMapping("/deleteFile")
    @ApiOperation(value = "删除图片接口")
//  @RequiresPermissions(PermissionConst.FILE_DELETE_FILE)//未发现调用
    public ApiRestResult deleteFile( String path){
        log.info("deleteFile=======who=delete={}", JwtUtil.getCurrentUser().getAcc());
        if(null == path || "".equals(path)){
            return ApiRestResult.err(CommonEnum.FAIL);
}
        try {
            fileService.delPicByUrl(path);
            return ApiRestResult.ok();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ApiRestResult.ok();
    }
}
